<template>
  <MainSlot>
    <template v-slot:searchForm>
      <SearchForm
        v-if="formItemList.length"
        :formItemList="formItemList"
        :unfoldFlag="unfoldFlag"
        @search="search"
        @isUnfold="
          (v) => {
            unfoldFlag = v;
          }
        "
      >
      </SearchForm>
    </template>
    <template v-slot:table>
      <TableData
        :tableThead="tableThead"
        :dataList="dataList"
        :loading="loading"
      >
        <template v-slot:bseal="{ row }">
          <span>{{ row.bseal ? "是" : "否" }}</span>
        </template>
      </TableData>
    </template>
    <template v-slot:pagination>
      <Pagination
        @current-change="handleCurrentChange"
        :page="page"
        :total="total"
      >
      </Pagination>
    </template>
  </MainSlot>
</template>
<script>
import MainSlot from "@/components-production-management/MainSlot";
import Pagination from "@/components-production-management/Pagination";
import SearchForm from "@/components-production-management/SearchForm";
import TableData from "@/components-production-management/TableData";
//NC批次
import ncQueryApi from "@/api/nc-query/index";

export default {
  components: { MainSlot, Pagination, SearchForm, TableData },
  data() {
    return {
      unfoldFlag: false,
      loading: false,
      page: 1,
      total: 10,
      searchFormData: {},
      dataList: [],
      tableThead: [
        { isShow: true, label: "批次名称", prop: "vbatchcode" },
        {
          isShow: true,
          label: "批次主键",
          prop: "pk_batchcode",
          minWidth: "120px",
        },
        {
          isShow: true,
          label: "物料名称",
          prop: "cmaterialvname",
          minWidth: "120px",
        },
        { isShow: true, label: "封存", slotName: "bseal", minWidth: "120px" },
        {
          isShow: true,
          label: "首次入库日期",
          prop: "dinbounddate",
          minWidth: "120px",
        },
      ],

      formItemList: [
        // {
        //   label: "批次主键",
        //   type: "input",
        //   param: "pk_batchcode",
        //   defaultSelect: "",
        //   multiple: false,
        //   isShow: true,
        // },
        {
          label: "日期查询",
          type: "datetimerange",
          param: "dateTime",
          multiple: false,
          isShow: true,
        },
      ],
    };
  },
  mounted() {
    this.queryPageByCondition();
  },
  methods: {
    queryPageByCondition() {
      let ncOrg = JSON.parse(localStorage.getItem("ncOrg")||"{}");
      let params = {
        page: this.page,
        size: 10,
        entity: {
          ncOrg,
          ...this.searchFormData,
        },
      };
      this.loading = true;
      ncQueryApi
        .queryBatchcode(params)
        .then((res) => {
          this.loading = false;
          if (res.success == 1) {
            this.dataList = res.data.content || [];
            this.total = res.data.totalElements;
            if (res.data.totalPages && this.page > res.data.totalPages) {
              this.page = res.data.totalPages;
              this.queryPageByCondition();
            }
          } else {
            this.$message.warning(res.message || "分页查询失败");
          }
        })
        .catch((err) => {
          this.loading = false;
          console.log(err);
        });
    },
    search(data) {
      this.page = 1;
      if (data.dateTime && data.dateTime.length) {
        data.startDate = data.dateTime[0];
        data.endDate = data.dateTime[1];
      }
      delete data.dateTime;
      this.searchFormData = { ...data };

      for (let key in this.searchFormData) {
        if (!this.searchFormData[key]) {
          this.searchFormData[key] = null;
        }
      }
      console.log(this.searchFormData, data, "this.searchFormData");
      this.queryPageByCondition();
    },
    handleCurrentChange(page) {
      this.page = page;
      this.queryPageByCondition();
    },
  },
};
</script>
<style lang="scss" scoped></style>